<template>
  <div class="login">
    <div class="title-wrapper">
      <div class="title"><img src="./logo.jpg"></div>
    </div>
    <div class="middle-wrapper">
      <div class="login-form">
        <div class="name">得金后台管理系统</div>
        <el-form
          ref="loginForm"
          :model="form"
          :rules="rules">
          <el-form-item prop="username">
            <el-input v-model="form.username" placeholder="请输入账号">
              <i slot="prefix" class="el-input__icon el-icon-adm-user" style="font-size: 18px;"></i>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input :type="passwordType" v-model="form.password" placeholder="请输入密码">
              <i slot="prefix" class="el-input__icon el-icon-adm-password" style="font-size: 18px;"></i>
              <i slot="suffix" class="el-input__icon el-icon-view" style="cursor: pointer;"
                 @click="_togglePasswordType"></i>
            </el-input>
          </el-form-item>
          <el-form-item prop="yanzhengma">
            <div class="check-code-wrapper">
              <div class="yanzhengma-wrapper">
                <el-input v-model="form.yanzhengma" @keyup.enter.native="login('loginForm')" placeholder="请输入验证码">
                  <i slot="prefix" class="el-input__icon el-icon-adm-vertification" style="font-size: 18px;"></i>
                </el-input>
              </div>
              <div class="validate-code-wrapper">
                <validate-code ref="validate-code" @change="_setCheckCode"></validate-code>
              </div>
            </div>
          </el-form-item>
          <el-form-item style="margin-bottom: 0;">
            <el-col :span="10" :offset="14">
              <el-form-item>
                <el-button
                  type="primary"
                  class="login-btn"
                  style="width: 100%;"
                  @click="loginHandle('loginForm')">
                  登陆系统
                </el-button>
              </el-form-item>
            </el-col>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="bottom">
      <p>深圳市晨雨消费养老服务有限公司 版权所有</p>
      <p><a href="http://www.beian.miit.gov.cn">粤ICP备19090532号 深圳市晨雨消费养老服务有限公司</a></p>
    </div>
  </div>
</template>
<script>
import validateCode from 'src/components/ValidateCode/index'
import { login } from '../../api/api'

export default {
  created () {
  },
  data () {
    var checkYanzhengma = (rule, value, callback) => {
      value = value.toUpperCase()
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else if (value !== this.checkCode) {
        callback(new Error('验证码错误'))
        this.$refs['validate-code'].draw()
      } else {
        callback()
      }
    }
    return {
      passwordType: 'password',
      checkCode: '',
      form: {
        username: '',
        password: '',
        yanzhengma: ''
      },
      rules: {
        username: [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        yanzhengma: [
          { validator: checkYanzhengma, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    _setCheckCode (value) {
      this.checkCode = value
    },
    _togglePasswordType () {
      if (this.passwordType === 'password') {
        this.passwordType = 'text'
      } else {
        this.passwordType = 'password'
      }
    },
    loginHandle (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.login()
        }
      })
    },
    login () {
      let token = ''
      let refreshToken = ''
      let userId = ''
      /*
       *  在这边可以进行登陆请求
       *  将请求返回的Token对象存到store中
       *  @Token  token对象
      */
      const para = Object.assign({}, {
        account: this.form.username,
        password: this.form.password
      })

      login(para).then(res => {
        token = res.data.token
        refreshToken = res.data.refreshToken
        userId = res.data.userId
        this.$store.commit('SET_TOKEN', token)
        this.$store.commit('SET_REFRESHTOKEN', refreshToken)
        this.$store.commit('SET_USERID', userId)
        this.$router.replace('home')
      })

      // let token = 'a94756da-2962-40ae-bdea-787fd02c9d92'
    }
  },
  components: {
    validateCode
  }
}
</script>
<style lang="scss" scoped>

  .login {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background: url("./background.jpg") no-repeat;
    background-size: 100% 100%;

    .title-wrapper {
      padding: 20px 0 5px 40px;
      background: #fff;
    }


    .middle-wrapper {
      position: fixed;
      width: 100%;
      margin: 0 auto;
      top: 50%;
      transform: translateY(-60%);

      .name {
        margin: 0 0 15px 0;
        text-align: center;
        font-weight: bold;
        font-size: 20px;
        color: #333;
      }

      .login-form {
        position: relative;
        margin: 0 auto;
        width: 520px;
        padding: 30px 50px;
        box-sizing: border-box;
        border-radius: 5px;
        background-color: #ffffff;

        .el-row {
          margin-bottom: 20px;

          &:last-child {
            margin-bottom: 0;
          }

          .login-btn {
            width: 100%;
          }
        }

        .check-code-wrapper {
          display: flex;
          justify-content: space-between;
          height: 40px;

          .yanzhengma-wrapper {
            flex: 0 1 auto;
          }

          .validate-code-wrapper {
            flex: 0 0 160px;
          }
        }
      }
    }

    .bottom {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      text-align: center;
      padding: 25px 0;
      font-size: 12px;
      background: #fff;

      p {
        margin: 0 0 15px 0;
        color: #333;


        a {
          text-decoration: none;
          color: #333;
        }
      }
    }
  }
</style>
